home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Source / GNU / cc / byteorder.h < prev    next >
C/C++ Source or Header  |  1993-06-04  |  3KB  |  134 lines

  1. #ifndef _SYS_BYTEORDER_H
  2. #define _SYS_BYTEORDER_H
  3.  
  4. /* Functions to convert `short' and `long' quantities from host byte order
  5.    to (internet) network byte order (i.e. big-endian).
  6.  
  7.    Written by Ron Guilmette (rfg@ncd.com).
  8.  
  9.    This isn't actually used by GCC.  It is installed by fixinc.svr4.
  10.  
  11.    For big-endian machines these functions are essentially no-ops.
  12.  
  13.    For little-endian machines, we define the functions using specialized
  14.    asm sequences in cases where doing so yields better code (e.g. i386).  */
  15.  
  16. #if !defined (__GNUC__) && !defined (__GNUG__)
  17. #error You lose!  This file is only useful with GNU compilers.
  18. #endif
  19.  
  20. #ifdef __GNUC__
  21. #define __STATIC static
  22. #else
  23. #define __STATIC
  24. #endif
  25.  
  26. #ifdef __STDC__
  27. __STATIC __inline__ unsigned long htonl (unsigned long);
  28. __STATIC __inline__ unsigned short htons (unsigned int);
  29. __STATIC __inline__ unsigned long ntohl (unsigned long);
  30. __STATIC __inline__ unsigned short ntohs (unsigned int);
  31. #endif /* defined (__STDC__) */
  32.  
  33. #if defined (__i386__)
  34.  
  35. /* Convert a host long to a network long.  */
  36.  
  37. /* We must use a new-style function definition, so that this will also
  38.    be valid for C++.  */
  39. __STATIC __inline__ unsigned long
  40. htonl (unsigned long __arg)
  41. {
  42.   register unsigned long __result;
  43.  
  44.   __asm__ ("xchg%B0 %b0,%h0\n\
  45.     ror%L0 $16,%0\n\
  46.     xchg%B0 %b0,%h0" : "=q" (__result) : "0" (__arg));
  47.   return __result;
  48. }
  49.  
  50. /* Convert a host short to a network short.  */
  51.  
  52. __STATIC __inline__ unsigned short
  53. htons (unsigned int __arg)
  54. {
  55.   register unsigned short __result;
  56.  
  57.   __asm__ ("xchg%B0 %b0,%h0" : "=q" (__result) : "0" (__arg));
  58.   return __result;
  59. }
  60.  
  61. #elif ((defined (__i860__) && !defined (__i860_big_endian__))    \
  62.        || defined (__ns32k__) || defined (__vax__)        \
  63.        || defined (__spur__) || defined (__arm__))
  64.  
  65. /* For other little-endian machines, using C code is just as efficient as
  66.    using assembly code.  */
  67.  
  68. /* Convert a host long to a network long.  */
  69.  
  70. __STATIC __inline__ unsigned long
  71. htonl (unsigned long __arg)
  72. {
  73.   register unsigned long __result;
  74.  
  75.   __result = (__arg >> 24) & 0x000000ff;
  76.   __result |= (__arg >> 8) & 0x0000ff00;
  77.   __result |= (__arg << 8) & 0x00ff0000;
  78.   __result |= (__arg << 24) & 0xff000000;
  79.   return __result;
  80. }
  81.  
  82. /* Convert a host short to a network short.  */
  83.  
  84. __STATIC __inline__ unsigned short
  85. htons (unsigned int __arg)
  86. {
  87.   register unsigned short __result;
  88.  
  89.   __result = (__arg << 8) & 0xff00;
  90.   __result |= (__arg >> 8) & 0x00ff;
  91.   return __result;
  92. }
  93.  
  94. #else /* must be a big-endian machine */
  95.  
  96. /* Convert a host long to a network long.  */
  97.  
  98. __STATIC __inline__ unsigned long
  99. htonl (unsigned long __arg)
  100. {
  101.   return __arg;
  102. }
  103.  
  104. /* Convert a host short to a network short.  */
  105.  
  106. __STATIC __inline__ unsigned short
  107. htons (unsigned int __arg)
  108. {
  109.   return __arg;
  110. }
  111.  
  112. #endif /* big-endian */
  113.  
  114. /* Convert a network long to a host long.  */
  115.  
  116. __STATIC __inline__ unsigned long
  117. ntohl (unsigned long __arg)
  118. {
  119.   return htonl (__arg);
  120. }
  121.  
  122. /* Convert a network short to a host short.  */
  123.  
  124. __STATIC __inline__ unsigned short
  125. ntohs (unsigned int __arg)
  126. {
  127.   return htons (__arg);
  128. }
  129.  
  130.  
  131. #undef __STATIC
  132.  
  133. #endif /* !defined (_SYS_BYTEORDER_H) */
  134.